<template>
    <el-dialog :title="form.userInfoId ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible"
        :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading">
        <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.openid')" prop="openid">
        <el-input v-model="form.openid" :placeholder="t('userInfo.inputOpenidTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.nickname')" prop="nickname">
        <el-input v-model="form.nickname" :placeholder="t('userInfo.inputNicknameTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.fullName')" prop="fullName">
        <el-input v-model="form.fullName" :placeholder="t('userInfo.inputFullNameTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.phoneNumber')" prop="phoneNumber">
        <el-input v-model="form.phoneNumber" :placeholder="t('userInfo.inputPhoneNumberTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.region')" prop="region">
        <el-input v-model="form.region" :placeholder="t('userInfo.inputRegionTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.detailedAddress')" prop="detailedAddress">
        <el-input v-model="form.detailedAddress" :placeholder="t('userInfo.inputDetailedAddressTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.coordinate')" prop="coordinate">
        <el-input v-model="form.coordinate" :placeholder="t('userInfo.inputCoordinateTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.avatarAddress')" prop="avatarAddress">
        <el-input v-model="form.avatarAddress" :placeholder="t('userInfo.inputAvatarAddressTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.userType')" prop="userType">
        <el-input v-model="form.userType" :placeholder="t('userInfo.inputUserTypeTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.createDate')" prop="createDate">
            <el-date-picker type="datetime" :placeholder="t('userInfo.inputCreateDateTip')" v-model="form.createDate" :value-format="dateTimeStr"></el-date-picker>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
  <el-form-item :label="t('userInfo.sort')" prop="sort">
    <el-input-number :min="1" :max="1000" v-model="form.sort" :placeholder="t('userInfo.inputSortTip')"></el-input-number>
  </el-form-item>
  </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.loginAccount')" prop="loginAccount">
        <el-input v-model="form.loginAccount" :placeholder="t('userInfo.inputLoginAccountTip')"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item :label="t('userInfo.password')" prop="password">
        <el-input v-model="form.password" :placeholder="t('userInfo.inputPasswordTip')"/>
      </el-form-item>
      </el-col>

    </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="CoalUserInfoDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/coalSystem/userInfo'
import { useI18n } from "vue-i18n"
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

const { t } = useI18n();

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
    openid: '',
    nickname: '',
    fullName: '',
    phoneNumber: '',
    region: '',
    detailedAddress: '',
    coordinate: '',
    avatarAddress: '',
    userType: '',
    createDate: '',
    sort: '',
    loginAccount: '',
    password: '',
});

// 定义校验规则
const dataRules = ref({
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.userInfoId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取coalUserInfo信息
  if (id) {
    form.userInfoId = id
    getcoalUserInfoData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
        loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(t(form.id ? 'common.editSuccessText' : 'common.addSuccessText'));
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
        loading.value = false;
    }
};

// 初始化表单数据
const getcoalUserInfoData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>